package edu.nps.ai.weapon;

import java.util.Date;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;


/**
 * @author Timothy Riley
 */
public class Weapon implements Comparable<Weapon> {
	/**
	 * If all Snit haven't been killed in this time try a different weapon
	 */
	private static final Long MAX_TIME_ALLOWED = 300000l;
	/**
	 * Holds the ordinal
	 */
	private Integer ordinal;
	/**
	 * Holds the name of the weapon
	 */
	private String name;
	/**
	 * Holds the location
	 */
	private String location;
	/**
	 * Holds true if the weapon affects all in the room
	 */
	private Boolean affectAll;
	/**
	 * Holds true if the weapon is a single use only
	 */
	private Boolean singleUse;
	/**
	 * Holds the effectiveness of the weapon
	 */
	private Integer effectiveness;
	/**
	 * Holds the time in millis the weapon was acquired
	 */
	private Long timeAcquired;
	/**
	 * @param name
	 * @param location
	 */
	public Weapon(Integer ordinal, String name, String location, String affectsAll, String singleUse) {
		this.ordinal = ordinal;
		this.name = name;
		this.location = location;
		this.affectAll = Boolean.valueOf(affectsAll);
		this.singleUse = Boolean.valueOf(singleUse);
		this.singleUse = false;
		this.effectiveness = null;
	}
	/**
	 * @return the ordinal
	 */
	public Integer getOrdinal() {
		return ordinal;
	}
	/**
	 * @param ordinal the ordinal to set
	 */
	public void setOrdinal(Integer ordinal) {
		this.ordinal = ordinal;
	}
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the location
	 */
	public String getLocation() {
		return location;
	}
	/**
	 * @param location the location to set
	 */
	public void setLocation(String location) {
		this.location = location;
	}
	/**
	 * @return the affectAll
	 */
	public Boolean getAffectAll() {
		return affectAll;
	}
	/**
	 * @param affectAll the affectAll to set
	 */
	public void setAffectAll(Boolean affectAll) {
		this.affectAll = affectAll;
	}
	/**
	 * @return the singleUse
	 */
	public Boolean getSingleUse() {
		return singleUse;
	}
	/**
	 * @param singleUse the singleUse to set
	 */
	public void setSingleUse(Boolean singleUse) {
		this.singleUse = singleUse;
	}
	/**
	 * @return the effectiveness
	 */
	public Integer getEffectiveness() {
		return effectiveness;
	}
	/**
	 * @param effectiveness the effectiveness to set
	 */
	public void setEffectiveness(Integer effectiveness) {
		this.effectiveness = effectiveness;
	}
	/**
	 * @return the timeAcquired
	 */
	public Long getTimeAcquired() {
		return timeAcquired;
	}
	/**
	 * @param timeAcquired the timeAcquired to set
	 */
	public void setTimeAcquired(Long timeAcquired) {
		this.timeAcquired = timeAcquired;
	}

	/**
	 * True if the weapon is expired
	 * @return
	 */
	public Boolean isExpired() {
		Boolean expired = false;
		if (timeAcquired != null) {
			Date now = new Date();
			Long currentTime = now.getTime();
			Long used = currentTime - timeAcquired;
			if (used >= MAX_TIME_ALLOWED) {
				expired = true;
			}
		}
		return expired;
	}
	/**
	 * Implementation of the hash code method
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(1, 31);
		hashCodeBuilder.append(this.name);
		return hashCodeBuilder.toHashCode();
	}

	/**
	 * Implementation of the is equals method
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (null == obj) {
			return false;
		}
		if (obj instanceof Weapon == false) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		Weapon compareWeapon = (Weapon) obj;
		EqualsBuilder equalsBuilder = new EqualsBuilder();
		equalsBuilder.append(this.name, compareWeapon.getName());
		return equalsBuilder.isEquals();
	}
	/**
	 * Implementation of compareTo
	 */
	public int compareTo(Weapon compareWeapon) {
		CompareToBuilder compareToBuilder = new CompareToBuilder();
		compareToBuilder.append(this.effectiveness, compareWeapon.getEffectiveness());
		compareToBuilder.append(this.ordinal, compareWeapon.getOrdinal());
		return compareToBuilder.toComparison();
	}

	/**
	 * Implementation of toString
	 */
	public String toString() {
		return name;
	}
}
